<?php
namespace home\Controller;
use Think\Controller;

class NoticeController extends ApibaseController
{

    function __construct()
    {
        parent::__construct();
    }

    /**
     * 公告管理
     */
    public function notice()
    {
        $page    = I('get.page', 1);
        $perpage = I('get.perpage', 10);
        $url     = [];
        $model   = M('Notice');
        $getData = $model->page($page, $perpage)->select();
        $this->assign('page', setPage($model->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $getData);
        $this->display('notice/notice');
    }

    /**
     * 添加公告
     * param face_img 封面图
     * param title 标题
     * param content 内容
     */
    public function addNotice()
    {
        if (I('post.postData')) {
            parse_str(htmlspecialchars_decode(I('post.postData')), $postData);
            if($postData['title'] && $postData['content']){
                $data['face_img']    = uploadQiniu(UPLOAD_PATH . $postData['face_img'], 'notice');
                $data['hospital_id'] = 1;
                $data['title']       = $postData['title'];
                $data['content']     = trim($postData['content']);
                $data['add_at']      = time();
                $data['update_at']   = time();
                $res                 = M('Notice')->add($data);
                if($res){
                    dataFormat('',0,'公告添加成功');
                }else{
                    dataFormat('',1,'添加失败');
                }
            }else{
                dataFormat('',1,'参数缺失');
            }
        }
    }

    /**
     * 修改公告
     * param notice_id 公告id
     */
    public function editNotice()
    {
        if(I('post.dosubmit')) {
            if (I('post.notice_id')) {
                $img      = I('post.img');
                $face_img = $img{0} == '/' ? uploadQiniu(UPLOAD_PATH . I('post.img'), 'notice') : $img;
                $data     = [
                    'face_img'  => $face_img,
                    'title'     => trim(I('post.title')),
                    'content'   => htmlspecialchars(I('post.content')),
                    'update_at' => time()
                ];
                $save     = M('notice')->where(['notice' => trim(I('post.notice_id'))])->setField($data);
                if ($save) {
                    $this->showtips('修改成功');
                } else {
                    $this->showtips('修改失败');
                }
            }
        }
        if (I('get.notice_id')){
            $data = M('Notice')->where(['notice_id'=>I('get.notice')])->find();
            $this->assign('notice_id',I('get.notice_id'));
            $this->assign('getData',$data);
            $this->display('notice/edit_notice');
        }
    }

    /**
     * 删除公告
     */
    public function delNotice()
    {
        if(I('get.notice_id')){
            $del = M('Notice')->where(['notice_id'=>I('get.notice_id')])->delete();
            if($del){
                dataFormat('',0,'删除成功');
            }else{
                dataFormat('',1,'删除失败');
            }
        }
    }
}